//
//  MCMSecureSettingTableViewCell.m
//  TLYL
//
//  Created by MC on 2017/7/11.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCMSecureSettingTableViewCell.h"
#import "MCQuestionView.h"

@interface MCMSecureSettingTableViewCell ()

@property (nonatomic,strong)UIButton * btn_Question1;
@property (nonatomic,strong)UIButton * btn_Question2;
@property (nonatomic,strong)UIButton * btn_Question3;

@property (nonatomic,strong)MCQuestionView * view_Question1;
@property (nonatomic,strong)MCQuestionView * view_Question2;
@property (nonatomic,strong)MCQuestionView * view_Question3;

@property (nonatomic,strong)UIButton * resetBtn;//充值
@property (nonatomic,strong)UIButton * saveBtn;//确定
@property (nonatomic,assign)int  isShowIndex;


@end

@implementation MCMSecureSettingTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    self.backgroundColor=[UIColor clearColor];
    [self setFooter];
    
    
    _isShowIndex=0;
#pragma mark==========问题1======================
    _btn_Question1 =[[UIButton alloc]init];
    _textField_Question1 =[[UITextField alloc]init];
    [self setQuestionWithBtn:_btn_Question1 andTF:_textField_Question1 andIndex:0];

#pragma mark==========问题2======================
    _btn_Question2 =[[UIButton alloc]init];
    _textField_Question2 =[[UITextField alloc]init];
    [self setQuestionWithBtn:_btn_Question2 andTF:_textField_Question2 andIndex:1];
    
#pragma mark==========问题3======================
    _btn_Question3 =[[UIButton alloc]init];
    _textField_Question3 =[[UITextField alloc]init];
    [self setQuestionWithBtn:_btn_Question3 andTF:_textField_Question3 andIndex:2];
    
    __weak __typeof(self)wself = self;

    _view_Question1=[[MCQuestionView alloc]init];
    _view_Question1.block=^(NSString *str){
        [wself setQuestion:str Index:0];
    };
    [self setQuestionWithIndex:0 andView:_view_Question1];

    _view_Question2=[[MCQuestionView alloc]init];
    _view_Question2.block=^(NSString *str){
        [wself setQuestion:str Index:1];
    };
    [self setQuestionWithIndex:1 andView:_view_Question2];

    _view_Question3=[[MCQuestionView alloc]init];
    _view_Question3.block=^(NSString *str){
        [wself setQuestion:str Index:2];
    };
    [self setQuestionWithIndex:2 andView:_view_Question3];

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeWTT:)];
    singleTapGesture.numberOfTapsRequired = 1;
    singleTapGesture.cancelsTouchesInView = NO;
    [self  addGestureRecognizer:singleTapGesture];
    
    
    
}

#pragma mark-创建问题
-(void)setQuestionWithBtn:(UIButton *)btn andTF:(UITextField *)textField andIndex:(int)index{
    
    UIView * view_back=[[UIView alloc]init];
    view_back.backgroundColor=[UIColor whiteColor];
    [self addSubview:view_back];
    view_back.frame=CGRectMake(0, 10+120*index, G_SCREENWIDTH, 110);
    
    [btn setTitle:[NSString stringWithFormat:@"(问题%d)请选择密保问题",index+1] forState:UIControlStateNormal];
    btn.titleLabel.font=[UIFont systemFontOfSize:14];
    btn.titleLabel.textColor=RGB(100, 100, 100);
    [view_back  addSubview:btn];
    btn.frame=CGRectMake(10,0, G_SCREENWIDTH, 45);
    
    if (index==0) {
        [btn addTarget:self action:@selector(action_showQuestion1) forControlEvents:UIControlEventTouchUpInside];
    }else if (index==1){
        [btn addTarget:self action:@selector(action_showQuestion2) forControlEvents:UIControlEventTouchUpInside];
    }else if (index==2){
        [btn addTarget:self action:@selector(action_showQuestion3) forControlEvents:UIControlEventTouchUpInside];
    }
    
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btn setTitleColor:RGB(100, 100, 100) forState:UIControlStateNormal];
    
    
    
    UIView * view_line1=[[UIView alloc]init];
    view_line1.backgroundColor=RGB(200, 200, 200);
    [view_back addSubview:view_line1];
    view_line1.frame=CGRectMake(0, 45, G_SCREENWIDTH, 0.5);
    
    [self setTextField:textField WithPlaceholder:@"请输入答案" and:UIKeyboardTypeDefault];
    [view_back addSubview:textField];
    textField.frame=CGRectMake(10, 45, G_SCREENWIDTH-20, 65);
    
    /*
     * 箭头
     */
    UIImageView * imgV_arrow1=[[UIImageView alloc]init];
    [view_back addSubview:imgV_arrow1];
    imgV_arrow1.image=[UIImage imageNamed:@"personal-more"];
    imgV_arrow1.frame=CGRectMake(G_SCREENWIDTH-10-6, 22.5-5.25, 12*0.5, 21*0.5);
}
#pragma mark - gesture actions
- (void)closeWTT:(UITapGestureRecognizer *)recognizer {
  
    [self endEditing:YES];
    
    [self closeWT];
    
    
}

-(void)closeWT{
    [UIView animateWithDuration:0.2 animations:^{
        
        _view_Question1.frame=CGRectMake(10, 45+10, G_SCREENWIDTH-20, 0);
        
        _view_Question3.frame=CGRectMake(10, 45+130+120, G_SCREENWIDTH-20, 0);
        
        _view_Question2.frame=CGRectMake(10, 45+130, G_SCREENWIDTH-20, 0);
        
    } completion:^(BOOL finished) {
        _isShowIndex=0;
    }];
}


-(void)setFooter{
#pragma mark==========footer======================
    
    //重置
    _resetBtn=[[UIButton alloc]init];
    [self setFooter:_resetBtn andTitle:@"重置"];
    [_resetBtn addTarget:self action:@selector(action_Reset) forControlEvents:UIControlEventTouchUpInside];
    
    //确定
    _saveBtn=[[UIButton alloc]init];
    [self setFooter:_saveBtn andTitle:@"确定"];
    [_saveBtn addTarget:self action:@selector(action_Save) forControlEvents:UIControlEventTouchUpInside];

    [_resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.width.mas_equalTo(G_SCREENWIDTH-20);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    [_saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.width.mas_equalTo(G_SCREENWIDTH-20);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(_resetBtn.mas_top).offset(-30);
    }];
    
}

-(void)setFooter:(UIButton *)btn andTitle:(NSString *)title{
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [btn setBackgroundImage:[UIImage imageNamed:@"Button_Determine"] forState:UIControlStateNormal];
    //    [btn setImage:[UIImage imageNamed:@"Button_Determin_Right"] forState:UIControlStateNormal];
    [self addSubview:btn];
    btn.layer.cornerRadius=10.0;
    btn.clipsToBounds=YES;
}



-(void)setQuestion:(NSString *)question Index:(int)index{
    NSMutableString *StringQuestion = [[NSMutableString alloc] initWithString:question];

    if (index==0) {
       
        [StringQuestion replaceCharactersInRange:NSMakeRange(0, 1) withString:@"1"];

        [_btn_Question1 setTitle:StringQuestion forState:UIControlStateNormal];
        [UIView animateWithDuration:0.2 animations:^{
            
            _view_Question1.frame=CGRectMake(10, 45+10, G_SCREENWIDTH-20, 0);
            
        } completion:^(BOOL finished) {
            
        }];
    }else if (index==1){
        
        [StringQuestion replaceCharactersInRange:NSMakeRange(0, 1) withString:@"2"];

        [_btn_Question2 setTitle:StringQuestion forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.2 animations:^{
            _view_Question2.frame=CGRectMake(10, 45+130, G_SCREENWIDTH-20, 0);
            
            
            
        } completion:^(BOOL finished) {
            
        }];
    }else if (index==2){
        
        [StringQuestion replaceCharactersInRange:NSMakeRange(0, 1) withString:@"3"];
        
        [_btn_Question3 setTitle:StringQuestion forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.2 animations:^{
            _view_Question3.frame=CGRectMake(10, 45+130+120, G_SCREENWIDTH-20, 0);
            
        } completion:^(BOOL finished) {
            
        }];
    }
    
    _isShowIndex=0;

}

-(void)setQuestionWithIndex:(int)index andView:(MCQuestionView*)view{
    
    [self addSubview:view];
    view.layer.cornerRadius=10;
    view.clipsToBounds=YES;
    view.frame=CGRectMake(10, 45+10+120*index, G_SCREENWIDTH-20, 0);
    view.dataSource=[NSMutableArray arrayWithArray:@[@"1.你最喜欢的XX?",@"2.你最喜欢的YY?",@"3.你最喜欢的ZZ?"]];
}

-(void)action_showQuestion1{
    if (_isShowIndex==1) {
        return;
    }
    _isShowIndex=1;
    _view_Question2.frame=CGRectMake(10, 45+130, G_SCREENWIDTH-20, 0);
    _view_Question3.frame=CGRectMake(10, 45+130+120, G_SCREENWIDTH-20, 0);
    
    [UIView animateWithDuration:0.2 animations:^{
        
        _view_Question1.frame=CGRectMake(10, 45+10, G_SCREENWIDTH-20, HEIGHT_MCQuestion);

    } completion:^(BOOL finished) {
        
    }];
}

-(void)action_showQuestion2{
    
    if (_isShowIndex==2) {
        return;
    }
    
    _isShowIndex=2;
    _view_Question1.frame=CGRectMake(10, 45, G_SCREENWIDTH-20, 0);
    _view_Question3.frame=CGRectMake(10, 45+130+120, G_SCREENWIDTH-20, 0);
    
    [UIView animateWithDuration:0.2 animations:^{
        _view_Question2.frame=CGRectMake(10, 45+130, G_SCREENWIDTH-20, HEIGHT_MCQuestion);
    } completion:^(BOOL finished) {
        
    }];
}

-(void)action_showQuestion3{
    if (_isShowIndex==3) {
        return;
    }
    
    _isShowIndex=3;
    

    _view_Question1.frame=CGRectMake(10, 45, G_SCREENWIDTH-20, 0);
    _view_Question2.frame=CGRectMake(10, 45+130, G_SCREENWIDTH-20, 0);
    
    [UIView animateWithDuration:0.2 animations:^{
        _view_Question3.frame=CGRectMake(10, 45+130+120, G_SCREENWIDTH-20, HEIGHT_MCQuestion);
        
    } completion:^(BOOL finished) {
        
    }];
}

-(void)setDataSource:(id)dataSource{
    
    _dataSource=dataSource;

    
}

+(CGFloat)computeHeight:(id)info{
    
    return 40+110*3 + 180;
    
}

-(void)textFieldDidChange:(UITextField *)textField{
    
}


-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type{
    textField.placeholder=placeholder;
    textField.borderStyle = UITextBorderStyleNone;
    textField.backgroundColor=[UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:12];
    textField.textColor = RGB(177, 177, 177);
    textField.textAlignment = NSTextAlignmentLeft;
    textField.returnKeyType = UIReturnKeyDone;
    textField.keyboardType = type;
    [textField setValue:RGB(177, 177, 177) forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];

    
}

/*
 * 重置
 */
-(void)action_Reset{

    [_btn_Question1 setTitle:@"(问题1)请选择密保问题" forState:UIControlStateNormal];
    [_btn_Question2 setTitle:@"(问题2)请选择密保问题" forState:UIControlStateNormal];
    [_btn_Question3 setTitle:@"(问题3)请选择密保问题" forState:UIControlStateNormal];

    _textField_Question1.text=@"";
    _textField_Question2.text=@"";
    _textField_Question3.text=@"";
    
    [self closeWT];
    
    if (self.block) {
        self.block(1);
    }
    
}


/*
 * 保存
 */
-(void)action_Save{
    //两个问题不可重复
    
    //请选择密保问题
    
    //答案为4-20个字符
    
    //
    if([_btn_Question1.titleLabel.text rangeOfString:@"请选择密保问题"].location !=NSNotFound){
        [SVProgressHUD showInfoWithStatus:@"请选择密保问题"];
        return;
    }
    if([_btn_Question2.titleLabel.text rangeOfString:@"请选择密保问题"].location !=NSNotFound){
        [SVProgressHUD showInfoWithStatus:@"请选择密保问题"];
        return;
    }
    
    if([_btn_Question3.titleLabel.text rangeOfString:@"请选择密保问题"].location !=NSNotFound){
        [SVProgressHUD showInfoWithStatus:@"请选择密保问题"];
        return;
    }
    
    if (_textField_Question1.text.length<1||_textField_Question1.text.length>20||_textField_Question2.text.length<1||_textField_Question2.text.length>20||_textField_Question3.text.length<1||_textField_Question3.text.length>20) {
        [SVProgressHUD showInfoWithStatus:@"答案为4-20个字符"];
        return;
    }
    
    
    if (self.block) {
        self.block(2);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
















